package com.crack.nga2.volley;

import com.android.volley.NetworkResponse;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HttpHeaderParser;
import com.crack.nga2.utils.GlobalConst;

import java.io.UnsupportedEncodingException;
import java.util.Map;

/**
 * 收藏请求
 * Created by Administrator on 2016/1/20.
 */
public class CollectRequest extends BaseRequest<String> {
    private Response.Listener<String> listener;
    private Map<String,String> params;

    public CollectRequest(Map<String, String> params, Response.Listener<String> listener) {
        super(Method.POST, GlobalConst.BASE_URL + "nuke.php?", new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        });
        this.listener = listener;
        this.params = params;
    }

    @Override
    public Map<String, String> getParams() {
        return params;
    }

    @Override
    protected Response<String> parseNetworkResponse(NetworkResponse response) {
        String json;
        try {
            json = new String(response.data, "GBK");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return Response.error(new VolleyError(e));
        }

        return Response.success(json, HttpHeaderParser.parseCacheHeaders(response));
    }

    @Override
    protected void deliverResponse(String response) {
        if (listener != null) {
            listener.onResponse(response);
        }
    }
}
